﻿using System.Collections.Generic;
using AvalonDock;
using System.Windows.Forms.Integration;
using System.Collections.ObjectModel;
using Genetic.Data;

namespace Genetic
{
    public partial class frmProperties : DockableContent, IContentForm
    {
        System.Windows.Forms.PropertyGrid propertyGrid = new System.Windows.Forms.PropertyGrid();

        public delegate void OptionsChangedType(PropertyBlock[] blocks);
        public OptionsChangedType OptionsChanged;

        public delegate void ContentChangedType(IContentForm content);

        public ContentChangedType ContentChanged;

        void OnOptionsChanged(PropertyBlock[] blocks)
        {
            if (OptionsChanged != null)
            {
                OptionsChanged(blocks);
            }
        }

        void OnContentChanged(IContentForm content)
        {
            if (ContentChanged != null)
            {
                ContentChanged(content);
            }
        }

        public frmProperties()
        {
            InitializeComponent();
            //Text = Properties.Strings.PropertiesText;

            propertyGrid.ToolbarVisible = false;
            propertyGrid.PropertySort = System.Windows.Forms.PropertySort.NoSort;
            propertyGrid.HelpVisible = false;
            propertyGrid.PropertyValueChanged += propertyGrid_PropertyValueChanged;
            propertyGrid.SelectedGridItemChanged += propertyGrid_SelectedGridItemChanged;

            WindowsFormsHost propGridHost = new WindowsFormsHost();
            propGridHost.Child = propertyGrid;
            Content = propGridHost;
        }

        public void ClearData()
        {
            if (propertyGrid.SelectedObject != null)
            {
                propertyGrid.SelectedObject = null;
            }
        }

        public void ShowData(ObservableCollection<TableBlock> blocks, int templateIndex)
        {
            if (blocks == null)
            {
                propertyGrid.SelectedObjects = null;
                return;
            }

            PropertyBlock[] propertyBlocks = new PropertyBlock[blocks.Count];
            for (int i = 0; i < blocks.Count; i++)
            {
                propertyBlocks[i] = new PropertyBlock(blocks[i].Block, Helper.Template.Data.Files[templateIndex].Blocks.Values[blocks[i].Block.TemplateIndex]);
            }

            propertyGrid.SelectedObjects = propertyBlocks;
            propertyGrid.ExpandAllGridItems();

            //ensure visibility of selected grid item
            propertyGrid.SelectedGridItem.Select();
        }

        void propertyGrid_PropertyValueChanged(object s, System.Windows.Forms.PropertyValueChangedEventArgs e)
        {
            if (e.ChangedItem.Value != e.OldValue)
            {
                OnOptionsChanged((PropertyBlock[])propertyGrid.SelectedObjects);
            }
        }

        void propertyGrid_SelectedGridItemChanged(object sender, System.Windows.Forms.SelectedGridItemChangedEventArgs e)
        {
            OnContentChanged(this);
        }

        public bool CanAdd()
        {
            if (propertyGrid.SelectedGridItem != null)
            {
                return propertyGrid.SelectedGridItem.Value is PropertySubOptions || (propertyGrid.SelectedGridItem.Parent != null && propertyGrid.SelectedGridItem.Parent.Value is PropertySubOptions);
            }

            return false;
        }

        public bool CanCopy()
        {
            return false;
        }

        public bool CanCut()
        {
            return false;
        }

        public bool CanPaste()
        {
            return false;
        }

        public bool CanDelete()
        {
            return false;
        }

        public bool CanSelectAll()
        {
            return false;
        }

        public void Add(int index)
        {
            if (propertyGrid.SelectedGridItem.Value is PropertySubOptions)
                propertyGrid.SelectedGridItem.GridItems[propertyGrid.SelectedGridItem.GridItems.Count - 1].Select();
            else if (propertyGrid.SelectedGridItem.Parent.Value is PropertySubOptions)
                propertyGrid.SelectedGridItem.Parent.GridItems[propertyGrid.SelectedGridItem.Parent.GridItems.Count - 1].Select();
        }

        public void Delete()
        {
            PropertyOption option = (PropertyOption)((PropertyOptionDescriptor)propertyGrid.SelectedGridItem.PropertyDescriptor).PropertyOption;
            object oldValue = option.Value;
            option.Value = "";

            propertyGrid_PropertyValueChanged(propertyGrid, 
                new System.Windows.Forms.PropertyValueChangedEventArgs(propertyGrid.SelectedGridItem, oldValue));
        }

        public bool CanAddMultiple()
        {
            return false;
        }

        public void SelectAll()
        {
        }

        public AddInfo[] MultipleAddDropDown()
        {
            return null;
        }

        public void Copy()
        {
        }

        public void Paste()
        {
        }

        public void Cut()
        {
        }

        public bool UseDocument()
        {
            return false;
        }
    }
}
